package com.zhiyou.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import java.util.List;

import com.zhiyou.dao.RectDao;
import com.zhiyou.model.Rect;
import com.zhiyou.util.DBUtils;
import com.zhiyou.util.PageBean;

public class RectDaoImpl implements RectDao{

	@Override
	public PageBean<Rect> findAll(int pc ,int ps) {
		PageBean<Rect> pb = new PageBean<>();
		String sq = "select count(*) from rect "; 
		Object ob[] = {};
		DBUtils db = new DBUtils();
		ResultSet r = db.select(sq, ob);
		pb.setPc(pc);
		pb.setPs(ps);
		int tr = 0;
		if(r==null){
		}else{
			try {
				while(r.next()){
					tr = r.getInt(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pb.setTr(tr);
		String sql = "select * from rect limit ?,? ";
		Object obj[] = {(pc-1)*ps ,ps};
		ResultSet rs = db.select(sql, obj);
		List<Rect> list = new ArrayList<>();
		if(rs == null ){
		}else{
			try {
				while(rs.next()){
					Rect rect = new Rect(rs.getInt(1),rs.getShort(2),rs.getInt(3),rs.getDouble(4),rs.getDate(5),rs.getString(6));
					
					list.add(rect);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pb.setBeanList(list);
		db.close();
		return pb;
	}

	@Override
	public Rect findById(int id) {
		String sql = "select * from rect where r_id = ?";
		Object obj[] = {id};
		DBUtils db =new DBUtils(); 
		ResultSet rs = db.select(sql, obj);
		if(rs ==null){
			db.close();
			return null;
		}
		try {
			while(rs.next()){
				Rect rect = new Rect(rs.getInt(1),rs.getShort(2),rs.getInt(3),rs.getDouble(4),rs.getDate(5),rs.getString(6));
				
				db.close();
				return rect;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.close();
		return null;
	}

	@Override
	public boolean delete(int id) {
		String sql = "delete from rect where r_id = ? ";
		Object obj[] = {id};
		DBUtils db =new DBUtils(); 
		int num = db.update(sql, obj);
		db.close();
		if(num==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean update(Object[] obj) {
		String sql = "update rect set flat_id = ?,tenant_id = ?,r_money = ?,r_remark = ? where r_id = ? ";
		DBUtils db =new DBUtils(); 
		int num = db.update(sql, obj);
		db.close();
		if(num==1){
			return true;
		}else{
			return false;
		}
	} 

	@Override
	public boolean add(Object[] obj) {
		String sql = "insert into rect value(null,?,?,?,now(),?)";
		DBUtils db =new DBUtils(); 
		int num = db.update(sql, obj);
		db.close();
		if(num==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public int[] dels(int[] id) {
		// TODO Auto-generated method stub
		String sql = "delete from rect where r_id = ? ";
		DBUtils db =new DBUtils(); 
		int nums[] = db.dels(sql, id);
		db.close();
		return nums;
	}

	
	@Override
	public PageBean<Rect> find(String flat, String tenant, String money, int pc, int ps) {
		DBUtils db = new DBUtils();
		PageBean<Rect> pb = new PageBean<>();
		pb.setPc(pc);
		pb.setPs(ps);
		StringBuffer sq = new StringBuffer("select count(*) from rect where 1=1 ");
		List<Object> list = new ArrayList<>();
		if(flat!=null&&!flat.trim().isEmpty()){
			sq.append(" and flat_id = ? ");
			list.add(flat);
		}
		if(tenant!=null&&!tenant.trim().isEmpty()){
			sq.append(" and tenant_id = ? ");
			list.add(tenant);
		}
		if(money!=null&&!money.trim().isEmpty()){
			sq.append(" and r_money > ? ");
			list.add(money);
		}
		int tr = 0;
		ResultSet r = db.select(sq.toString(), list.toArray());
		if(r==null){
			pb.setTr(0);
		}else{ 
			try {
				while(r.next()){
					tr = r.getInt(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			pb.setTr(tr);
		}
		StringBuffer sql = new StringBuffer(sq.toString().replace("count(*)", "*"));
		sql.append(" limit ?,?");
		list.add((pc-1)*ps);
		list.add(ps);
		List<Rect> list1 = new ArrayList<>();
		ResultSet rs = db.select(sql.toString(), list.toArray());
		if (rs==null) {
			
		}else{
			try {
				while(rs.next()){
					Rect rect = new Rect(rs.getInt(1),rs.getShort(2),rs.getInt(3),rs.getDouble(4),rs.getDate(5),rs.getString(6));
					list1.add(rect);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pb.setBeanList(list1);
		db.close();
		return pb;
	}
	

}
